package cn.qidisoft.edu.hzjt.service.zyc.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.qidisoft.core.common.code.QdfRespCode;
import cn.qidisoft.core.common.exception.dao.DBException;
import cn.qidisoft.core.common.log.LoggerAdapter;
import cn.qidisoft.core.common.log.LoggerAdapterFactory;
import cn.qidisoft.core.common.pagination.Page;
import cn.qidisoft.core.common.pagination.PaginationBean;
import cn.qidisoft.edu.hzjt.dao.zyc.DyxfzsMapper;
import cn.qidisoft.edu.hzjt.dao.zyc.PartyBranchStarMapper;
import cn.qidisoft.edu.hzjt.model.Semester;
import cn.qidisoft.edu.hzjt.model.zyc.PartyBranchStar;
import cn.qidisoft.edu.hzjt.model.zyc.PartyBranchStarScore;
import cn.qidisoft.edu.hzjt.model.zyc.PartyLeagueMember;
import cn.qidisoft.edu.hzjt.model.zyc.PartyMemberEvaluation;
import cn.qidisoft.edu.hzjt.service.zyc.IPartyBranchStarService;
import cn.qidisoft.edu.hzjt.utils.LogType;

@Service
public class PartyBranchStarServiceImpl implements IPartyBranchStarService {
	protected static final LoggerAdapter LOGGER = LoggerAdapterFactory.getLogger(LogType.HZJT_LOGGER.val);

	@Autowired
	private PartyBranchStarMapper partyBranchStarMapper;

	@Autowired
	private DyxfzsMapper dyxfzsMapper;

	// 分页查询所有
	@Override
	public PaginationBean<PartyBranchStar> queryPartyBranchStarPageList(Map<String, Object> queryMap, Page page) {
		try {

			int c = partyBranchStarMapper.queryPartyBranchStarPageCount(queryMap);
			int totalRecords = 0;
			totalRecords = (int) c;

			PaginationBean<PartyBranchStar> pageHolder = new PaginationBean<PartyBranchStar>(page, totalRecords);

			List<PartyBranchStar> resultList = partyBranchStarMapper.queryPartyBranchStarPageList(queryMap,
					new RowBounds((pageHolder.getCurrentPage() - 1) * pageHolder.getPageSize(),
							pageHolder.getPageSize()));
			pageHolder.setPageList(resultList);
			return pageHolder;
		} catch (Exception e) {
			e.printStackTrace();
			LOGGER.error("queryStudentDemoPageList [{}] error," + this.getClass().getName() + "=[{}]",
					this.getClass().getName(),
					ToStringBuilder.reflectionToString(queryMap, ToStringStyle.SHORT_PREFIX_STYLE));
			throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
		}

	}

	@Override
	public List<PartyBranchStar> queryPartyBranchStarList() {
		return partyBranchStarMapper.queryPartyBranchStarList();
	}

	// 根根据年份得到学期id
	@Override
	public List<Semester> selXqid(String c_year) {
		List<Semester> semesters = new ArrayList<Semester>();
		for (int i = 1; i < 3; i++) {
			Semester semester = partyBranchStarMapper.selXqid(Integer.parseInt(c_year + i));
			semesters.add(semester);
		}
		return semesters;
	}

	@Override
	public List<PartyMemberEvaluation> getXfdyScore(List<PartyLeagueMember> leagueMembers, Integer xqid) {
		List<PartyMemberEvaluation> partyMemberEvaluations = new ArrayList<PartyMemberEvaluation>();
		Map<String, Object> qMap = new HashMap<String, Object>();
		for (int i = 0; i < leagueMembers.size(); i++) {
			leagueMembers.get(i).getdYID();
			qMap.put("dYID", leagueMembers.get(i).getdYID());
			qMap.put("semId", xqid);
			PartyMemberEvaluation partyMemberEvaluation = dyxfzsMapper.getXfdyScore(qMap);
			if (null != partyMemberEvaluation) {
				partyMemberEvaluations.add(partyMemberEvaluation);
			}
		}
		return partyMemberEvaluations;
	}

	// 添加数据
	@Override
	public Integer insert(PartyBranchStar data) {
		return partyBranchStarMapper.insert(data);
	}

	// 根據id得到单条数据信息
	@Override
	public PartyBranchStar selPartyBranchStarWithPartyBranch(Integer khid) {
		return partyBranchStarMapper.selPartyBranchStarWithPartyBranch(khid);
	}

	// 修改数据
	@Override
	public Integer update(PartyBranchStar data) {
		return partyBranchStarMapper.update(data);
	}

	// 删除数据
	@Override
	public Integer deleteByIds(List<Integer> id_List) {
		return partyBranchStarMapper.deleteByIds(id_List);
	}

	// 导出
	@Override
	public List<PartyBranchStar> getDASList(Map<String, Object> pM2) {
		return partyBranchStarMapper.getDASList(pM2);
	}

	@Override
	public PartyBranchStar selStarsByYearAndDzbid(PartyBranchStar partyBranchStar) {
		return partyBranchStarMapper.selStarsByYearAndDzbid(partyBranchStar);
	}

	// 根據年份和党支部id查分数
	@Override
	public HashMap<String, Object> selScoreByYaerAndDzbid(Map<String, Object> queryMap) {
		return partyBranchStarMapper.selScoreByYaerAndDzbid(queryMap);
	}

	// 查询所有semesters
	@Override
	public List<Semester> querySemesterList() {
		return partyBranchStarMapper.querySemesterList();
	}

	// 查询分数
	@Override
	public List<PartyBranchStarScore> selScore(Map<String, Object> queryMap) {
		return partyBranchStarMapper.selScore(queryMap);
	}

	// 查询党支部id
	@Override
	public PartyLeagueMember selDzbId(Integer emid) {
		return partyBranchStarMapper.selDzbId(emid);
	}

	@Override
	public PartyBranchStar getStar(PartyBranchStar pStar) {
		
		return partyBranchStarMapper.getStar(pStar);
	}

}
